package utils;

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class IFileUtil {
	private static final File sd = Environment.getExternalStorageDirectory();
	public static final String SD_PATH = sd.getPath();
	public static final String PACKAGE = "com.ebo.study";
	public static final String DIR = SD_PATH + "/" + PACKAGE + "/";

	static {
		File file = new File(DIR);
		if (!file.exists())
			file.mkdir();

	}
	// String Name = File.getName(); //获得文件或文件夹的名称：
	// String parentPath = File.getParent(); //获得文件或文件夹的父目录
	// String path = File.getAbsoultePath();//绝对路经
	// String path = File.getPath();//相对路经
	// File.createNewFile();//建立文件
	// File.mkDir(); //建立文件夹
	// File.isDirectory(); //判断是文件或文件夹
	// File[] files = File.listFiles(); //列出文件夹下的所有文件和文件夹名
	// File.renameTo(dest); //修改文件夹和文件名
	// File.delete(); //删除文件夹或文件

	public static String res;



	public static void write(String filename, String filecontent) {
		filename = DIR + filename;

		try {
			File file = new File(filename);
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(filecontent.getBytes());
			fos.close();
			fos = null;
		} catch (Exception e) {
			Lg.d("write Exception"+e);
			e.printStackTrace();
		}
		Lg.d("write sssssssssss");
	}
//	public static void write(String filePath,String filecontent) {
//		try {
//			File file = new File(DIR +filePath);
//			PrintStream ps = new PrintStream(new FileOutputStream(file));
////			ps.println("http://www.jb51.net");// 往文件里写入字符串
//			ps.append(filecontent);// 在已有的基础上添加字符串
//		} catch (FileNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	public static void delete(String filename) {
		try {
			File file = new File(filename);
			file.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	public static String read(String filename) {
		filename = DIR + filename;
		if (!isExist(filename)) {
			Lg.d("read 1111111111");
			return null;
		}
		try {
			File file = new File(filename);
			FileInputStream is = new FileInputStream(file);
			byte[] b = new byte[is.available()];
			is.read(b);
			String result = new String(b);
			Lg.d("read 222222222222222");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		Lg.d("read 33333333333333");
		return null;
	}

	public static boolean isExist(String fileName) {
		return new File(DIR + fileName).exists();
	}

	public static void copyfile(File fromFile, File toFile, Boolean rewrite) {
		if (!fromFile.exists()) {
			return;
		}
		if (!fromFile.isFile()) {
			return;
		}

		if (!fromFile.canRead()) {
			return;
		}

		if (!toFile.getParentFile().exists()) {
			toFile.getParentFile().mkdirs();
		}

		if (toFile.exists() && rewrite) {
			toFile.delete();
		}

		// 当文件不存时，canWrite一直返回的都是false

		// if (!toFile.canWrite()) {

		// MessageDialog.openError(new Shell(),"错误信息","不能够写将要复制的目标文件" +
		// toFile.getPath());

		// Toast.makeText(this,"不能够写将要复制的目标文件", Toast.LENGTH_SHORT);

		// return ;

		// }

		try {

			FileInputStream fosfrom = new FileInputStream(
					fromFile);

			FileOutputStream fosto = new FileOutputStream(toFile);

			byte bt[] = new byte[1024];

			int c;

			while ((c = fosfrom.read(bt)) > 0) {

				fosto.write(bt, 0, c); // 将内容写到新文件当中

			}

			fosfrom.close();

			fosto.close();

		} catch (Exception ex) {

			Lg.d("readfile  " + ex.getMessage());

		}

	}
}
